import core.ui.Event;
import xcms.DataBus;

class xcms.Initializer
{
    /**
     * Inits CMS application and plugins.
     *
     * Example:
     *
     * xcms.init( {
     * 		'id' : 1001,
     * 		'plugins' : {
     * 			'auth' : 'xcms.plugins.auth.AuthPlugin',
     * 			...
     * 		}
     * } );
     *
     * @param config (object) Site config.
     */
    this.init = function( config )
    {
        console.info( 'Initializing CMS application' );
        if ( defined( config.plugins ) )
        {
            for ( var name in config.plugins )
            {
                console.info( 'Initializing ' + name + ' plugin' );
                var pluginClass = DefaultClassLoader.getClassByName( config.plugins[name] );
                new pluginClass(); // Just create plugin object, that's all!
            }
        }
        xcms.publish( 'onCmsInit', [ config ] );
    }
}

/**
 * Built-in package function.
 *
 * @param config (object) Site config.
 * @see CmsApplication.init
 */
xcms.init = function( config )
{
    core.ui.Event.addLoadListener(function() {
        new Initializer().init( config );
    });
}